// 导入请求工具
import request from '../utils/request'

// 创建主评论（可带评分）
export function createReview(data: {
  itemId: number
  userId: number
  userName?: string
  userPic?: string
  content: string
  score?: number
}) {
  return request.post('/reviews', data)
}

// 回复评论（子评论）
export function replyReview(data: {
  parentId: string
  itemId: number
  userId: number
  userName?: string
  userPic?: string
  replyUserId?: number
  replyUserName?: string
  content: string
}) {
  return request.post('/reviews/reply', data)
}

// 删除评论
export function deleteReview(id: string) {
  return request.delete(`/reviews/${id}`)
}

// 点赞或取消点赞（increment 传 1 或 -1）
export function likeReview(id: string, increment: number = 1, userId: number) {
  return request.post(`/reviews/${id}/like?increment=${increment}&userId=${userId}`)
}

// 查询用户是否点赞了指定评论
export function getLikeStatus(reviewId: string, userId: number) {
  return request.get(`/reviews/${reviewId}/like-status?userId=${userId}`)
}

// 获取某商品的评论树（分页仅作用于顶层评论）
// 商品详情页：顶层评论分页，每条仅返回前两条子回复作为预览
export function getTopWithPreview(itemId: number, page: number = 1, size: number = 10) {
  return request.get(`/reviews/item/${itemId}/scroll`, { params: { page, size } })
}

// 顶层评论数量
export function countTopReviews(itemId: number) {
  return request.get(`/reviews/item/${itemId}/count`)
}

// 分页获取某条顶层评论的子回复
export function getReplies(parentId: string, page: number = 1, size: number = 10) {
  return request.get(`/reviews/${parentId}/replies`, { params: { page, size } })
}

// 获取某条顶层评论的子回复总数
export function countReplies(parentId: string) {
  return request.get(`/reviews/${parentId}/replies/count`)
}

// 根据 id 获取评论
export function getReviewById(id: string) {
  return request.get(`/reviews/${id}`)
}

// 获取某商品的各星级数量（1-5星，仅统计顶层评分）
export function getScoreStats(itemId: number) {
  return request.get(`/reviews/item/${itemId}/score-stats`)
}

export interface ReviewNode {
  userName: string
  id: string
  userId: number
  userPic?: string
  itemId: number
  parentId?: string
  content: string
  score?: number
  replyUserId?: number
  replyUserName?: string
  likeCount?: number
  createTime: string
  level: number
  children?: ReviewNode[]
  hasMoreChildren?: boolean  // 是否有更多子评论
}


